import api from '@/api/collect';
import { Message } from 'element-ui';
const collect = {
    state: {
        goodsDatas: {
            myCollectGoods: []
        },
        collectShop: [],
        pagination: {
            total:  null, //总共有几个
            pageNum: 1, //当前页数
            pageSize: 12, //一个展示几个
        },
        pagination2: {
            total:  null, //总共有几个
            pageNum: 1, //当前页数
            pageSize: 7, //一个展示几个
        },
        isCollect: false
    },

    mutations: {
        saveGoods: (state, payload) => {
            state.goodsDatas.myCollectGoods = [];
            payload.list.forEach(item => {
                item.goods_id = item.collect2goods_goodsid,
                item.selected = false;
                payload.num.forEach(nums => {
                    if(item.goods_id === nums.goods_id){
                        Object.assign(item,{...nums});
                    }
                })
            });
            state.goodsDatas.myCollectGoods = [...payload.list];
        },
        saveShops: (state, { collectShopMap, collectShopGoodsMap }) => {
            state.collectShop = [];
            collectShopMap.forEach(shop => {
                state.collectShop.push({
                    ...shop,
                    selected: false,
                    goods: []
                });
            });
            collectShopGoodsMap.forEach(good => {
                state.collectShop.forEach(shop => {
                    if(shop.shop_id === good.shop_id){
                        shop.goods.push(good);
                    }
                })
            });
        },
        savePagination: (state, paylaod) => {
            state.pagination = Object.assign(state.pagination,paylaod);
        },
        savePagination2: (state, paylaod) => {
            state.pagination2 = Object.assign(state.pagination2,paylaod);
        },
        changeCollect: (state, isCollect) => {
            state.isCollect = isCollect;
        }
    },

    actions: {
        //收藏店铺
        async fetchCollectAddShop({ commit },payload) {
            const { code, extend: { message } } = await api.collectShop(payload);
            if(code === 200){
                Message({
                    showClose: true,
                    message: '该店已添加至收藏夹。',
                    type: 'success',
                });
                commit('changeCollect',1);
            }else{
                Message({
                    showClose: true,
                    message: message || '收藏店铺失败, 请重试。',
                    type: 'error',
                });
            }
        },
        async fetchIsCollectShop({ commit },payload) {
            const { code, extend: { result: { isCollect } } } = await api.userIsCollectShop(payload);
            if(code === 200){
                commit('changeCollect',isCollect);
            }
        },
        //获得收藏的商品
        async fetchCollectGoods({ commit },payload) {
            const { code, extend: { result } } = await api.selectCollectGoods(payload);
            if(code === 200){
                commit('saveGoods',{list: result.collectGoodsList.list, num: result.goodsNum })
                commit('savePagination', {
                    total:  result.collectGoodsList.total,  //总共有几个
                    pageNum: result.collectGoodsList.pageNum,  //当前页数
                });
            }
        },
        //获得收藏的店铺
        async fetchCollectShop({ commit },payload) {
            const { code, extend: { result } } = await api.selectCollectShop(payload);
            if(code === 200){
                commit('saveShops',{ 
                    collectShopMap: result.shopList.list[0], 
                    collectShopGoodsMap: result.shopList.list[1]
                });
                commit('savePagination2', {
                    total:  result.shopList.total,  //总共有几个
                    pageNum: result.shopList.pageNum,  //当前页数
                });
            }
        },
        // 宝贝移出收藏
        async fetchDeleteGoods({ commit },payload) {
            const { code } = await api.deleteMoreGoods(payload);
            if(code === 200){
                Message({
                    showClose: true,
                    message: '已移出收藏夹。',
                    type: 'success',
                });
                commit('changeIsCollected', 0);
            }else{
                Message({
                    showClose: true,
                    message: '移出收藏夹失败, 请重试。',
                    type: 'error',
                });
            }
        },
        // 店铺移出收藏
        async fetchDeleteShop({ commit },payload) {
            const { code } = await api.deleteMoreShop(payload);
            if(code === 200){
                Message({
                    showClose: true,
                    message: '已取消该商家的收藏。',
                    type: 'success',
                });
                commit('changeCollect',0);
            }else{
                Message({
                    showClose: true,
                    message: '取消收藏失败, 请重试。',
                    type: 'error',
                });
            }
        },
    }
}

export default collect